/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WISHMASTER_DEFAULTAGENT_H
#define WISHMASTER_DEFAULTAGENT_H

#include <QObject>
#include "chanagent.h"
#include "misc.h"

namespace Wishmaster
{
	class CaptchaFetcher;
	class TaskRequest;
	class TaskBoardRequest;
	class TaskThreadRequest;
	class TaskThumbnailRequest;
	class TaskAttachmentRequest;
	class WISHMASTER_EXPORT DefaultAgent : public ChanAgent
	{
		Q_OBJECT
	public:
		DefaultAgent(const QString& host);
		virtual ~DefaultAgent() {}
		virtual void setTaskhub(Taskhub* taskhub);
		virtual Taskhub* taskhub();

		virtual void setRequestManager(Wishmaster2::Network::RequestManager* manager);
		virtual Wishmaster2::Network::RequestManager* requestManager() const;

		virtual void setCaptchaFetcher(CaptchaFetcher* fetcher);
		virtual CaptchaFetcher* captchaFetcher() const;

		virtual void getCaptcha(const ThreadId& threadId);
		virtual void makePost(const PostDescriptor& post, const QString& captcha)  {}
		virtual void createThread(const PostDescriptor& post, const QString& captcha) {}
		virtual QStringList contentRatings() { return QStringList(); }

		virtual void requestFavicon();
		virtual void requestThread(const ThreadId& threadId, const PostId& lastPostId);
		virtual void requestThumbnail(const PostId& postId, int attachmentIndex, const QUrl& thumblink);
		virtual void requestPic(const PostId& postId, int attachmentIndex, const QUrl& piclink);
		virtual void requestBoardIndex(const QString& boardId, int page);
		virtual void downloadFile(const PostId& postId, int attachIndex, const QUrl& filelink, const QString& filename);

		virtual void deleteThread(const ThreadId& threadId, const QString& password);
		virtual void deletePost(const PostId& postId, const QString& password);

		void addAttachments(Wishmaster2::Network::RequestPtr& r, const AttachmentDescriptor& attach, const QString& fieldname);

		virtual ChanAgent* clone(const QString& host);
		virtual QString id();

		QString makeThreadLink(const ThreadId& threadId);

	protected slots:
		void slot_board(TaskBoardRequest* task, const QByteArray& arr);
		void slot_thread(TaskThreadRequest* task, const QByteArray& arr);
		void slot_thumb(TaskThumbnailRequest* task, const QByteArray& arr);
		void slot_pic(TaskAttachmentRequest* task, const QByteArray& arr, const QString& contentType);
		void slot_file(int status, const PostId& postid, int attachmentIndex, const QByteArray& arr);
		void slot_captchaFetcherResult(const ThreadId& threadid, const QByteArray& arr);
		void slot_mainpage(TaskRequest* task, const QByteArray& arr, const QString& contentType);
		void slot_favicon(TaskRequest* task, const QByteArray& arr, const QString& contentType);


	protected:
		QString makeFullHostname(const QString& board);

	private:
		Wishmaster2::Network::RequestManager* m_requestManager;
		QString m_host;
		Taskhub* m_taskhub;
		CaptchaFetcher* m_captchaFetcher;
		QString m_pageformat;
	};
}

#endif

